package edu.arizona.mindseye.swing.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import edu.arizona.mindseye.event.Event;
import edu.arizona.mindseye.event.EventManager;
import edu.arizona.mindseye.event.enums.EventType;
import edu.arizona.mindseye.event.enums.Parameter;

public class SeekListener implements ActionListener,ChangeListener {
	
	private int _index;
	
	public SeekListener(int index) { 
		_index = index;
	}

	@Override
	public void stateChanged(ChangeEvent arg0) {
		Object source = arg0.getSource();
		if (source instanceof JSlider) { 
			JSlider slider = (JSlider) source;
			if (slider.getValue() != _index) { 
				_index = slider.getValue();
				Event e = new Event(EventType.SEEK_COMMAND);
				e.addParameter(Parameter.index, _index);
				EventManager.inst().dispatch(e);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Event e = new Event(EventType.SEEK_COMMAND);
		e.addParameter(Parameter.index, _index);
		EventManager.inst().dispatch(e);
	}
	
}
